<template>
	<div>
		<van-address-list v-model="chosenAddressId" 
			:list="addressList" 
			default-tag-text="默认" 
			@add="onAdd" 
			@edit="onEdit" 
			@select="onSelect" 
		/>
	</div>
</template>

<script>
//获取地址列表
import { getAddressListAPI } from '@/api/address.js';

export default {
	data() {
		return {
			chosenAddressId: null,
			addressList: []
		};
	},

	created() {
		this.chosenAddressId = this.$route.params.addressId || '';
		this.getAddressList();
	},

	methods: {
		getAddressList() {
			getAddressListAPI().then(result => {
				result.data.forEach(item => {
					this.addressList.push({
						id: item.id,
						name: item.name,
						tel: item.tel,
						address: item.city + item.county + item.address_detail,
						isDefault: item.is_default == 1 ? true : false
					});
				});
			});
		},

		// 触发增加
		onAdd() {
			this.$router.push({
				name: 'addressEdit'
			});
		},

		// 触发编辑
		onEdit(item) {
			this.$router.push({
				name: 'addressEdit',
				params: {
					id: item.id
				}
			});
		},

		//处理点击选择列表
		onSelect(item) {
			let addressId = item.id;
			//路由有参数表明是要选择地址id的,把选择结果路由传参返回。
			if (this.$route.params.addressId) {
				this.$router.replace({
					name: 'orderCreate',
					params: {
						addressId
					}
				});
			}
		}
	}
};
</script>

<style></style>
